﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using JobCreateInDocs3;

namespace JobCreateInDocs3_WinFormsTest
{

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {

            if (Ko2.Sys.Net.NetInfo.MachineName.ToLower() != "s-wss02")
                return;

            int i = 0;
            i++;

            JobCreateInDocs3.JobCreateInDocs_Process.Execute(Guid.NewGuid());
        }

        private void jobInstToolStripMenuItem_Click(object sender, EventArgs e)
        {

            SPWebApplication webApp = SPWebApplication.Lookup(new Uri(@"http://localhost/sites/shop"));
            SPServer srv = new SPServer("S-WSS02");

            //JobCreateInDocs myTJ = new JobCreateInDocs(@"JobCreateInDocs_v3_4", webApp, srv, SPJobLockType.None);
            JobCreateInDocs myTJ = new JobCreateInDocs(@"JobCreateInDocs_v3_10", webApp);

            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 1;
            //SPSchedule schedule = SPSchedule.FromString("every 20 minutes between 7:00:00 and 23:00:00");
            myTJ.Schedule = schedule;
            myTJ.Update();

        }

        private void jobDelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SPWebApplication webApp = SPWebApplication.Lookup(new Uri(@"http://localhost/sites/shop"));
            SPServer srv = new SPServer("S-WSS02");
            foreach (SPJobDefinition job in webApp.Sites.WebApplication.JobDefinitions)
            {
                if (
                    job.Name == "JobCreateInDocs_v3_1"
                    || job.Name == "JobCreateInDocs_v3_2"
                    || job.Name == "JobCreateInDocs_v3_3"
                    || job.Name == "JobCreateInDocs_v3_4"
                    || job.Name == "JobCreateInDocs_v3_5"
                    || job.Name == "JobCreateInDocs_v3_8"
                    || job.Name == "JobCreateInDocs_v3_9"
                    || job.Name == "JobCreateInDocs_v3_10"
                    )
                {
                    job.Delete();
                }
            }
        }

        private void listDelAllItemsToolStripMenuItem_Click(object sender, EventArgs e)
        {

            using (SPSite site = new SPSite(@"http://localhost/sites/shop"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists[@"Первичные документы"];
                    for(int i = list.Items.Count-1; i>=0; i--)
                    {
                        list.Items[i].Delete();
                    }
                }
            }

        }

        private void setItemRightToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (SPSite site = new SPSite(@"http://localhost/sites/shop"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists[@"Первичные документы"];
                    Ko2.Portal.Right.PermDocRight.PermDocs_SetRoleItem(list.Items[0]);
                }
            }
        }

        private void executeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            JobCreateInDocs3_TestWinForm.JobCreateInDocs_Process.Execute(Guid.NewGuid());
        }

    }
}
